package com.wsd.smartcarwasher.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wsd.smartcarwasher.domain.LogDO;
import com.wsd.smartcarwasher.domain.PageDO;
import com.wsd.smartcarwasher.domain.Pagination;
import com.wsd.smartcarwasher.service.LogService;
import com.wsd.smartcarwasher.util.Query;
import com.wsd.smartcarwasher.util.R;
import com.wsd.smartcarwasher.util.ReturnUtil;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.aspectj.weaver.tools.cache.AsynchronousFileCacheBacking.RemoveCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;


@Api(tags="日志管理")
@RequestMapping("/common/log")
@Controller
@CrossOrigin
public class LogController {
	@Autowired
	LogService logService;
	@RequiresPermissions("common:log:list")
	@ResponseBody
	@GetMapping("/list")
	ModelMap list(@RequestParam int Page, @RequestParam int PageSize,
				  @RequestParam(required = false) String username,
				  @RequestParam(required = false) String gmtCreate,
				  @RequestParam(required = false) String ip
				  ) {
		Map<String,Object> params = new HashMap<>();
		params.put("Page",Page);
		params.put("PageSize",PageSize);
		params.put("username",username);
		params.put("ip",ip	);
		params.put("gmtCreate",gmtCreate);
		params.put("type",0	);
		Query query = new Query(params);
		PageDO<LogDO> page = logService.queryList(query);
		return ReturnUtil.Success("查询成功",new Pagination<LogDO>(Page,PageSize,page.getTotal(),page.getRows()));
	}


	@RequiresPermissions("common:log:userlist")
	@ResponseBody
	@GetMapping("/userlist")
	ModelMap userlist(@RequestParam int Page, @RequestParam int PageSize,
				  @RequestParam String userId,
				  @RequestParam(required = false) String gmtCreate,
				  @RequestParam(required = false) String ip
	) {
		Map<String,Object> params = new HashMap<>();
		params.put("Page",Page);
		params.put("PageSize",PageSize);
		params.put("userId",userId);
		params.put("ip",ip	);
		params.put("gmtCreate",gmtCreate);
		params.put("type",1	);
		Query query = new Query(params);
		PageDO<LogDO> page = logService.queryList(query);
		return ReturnUtil.Success("查询成功",new Pagination<LogDO>(Page,PageSize,page.getTotal(),page.getRows()));
	}

	@RequiresPermissions("common:log:remove")
	@ResponseBody
	@PostMapping("/remove")
	R remove(Long id) {
		if (logService.remove(id)>0) {
			return R.ok();
		}
		return R.error();
	}
	@RequiresPermissions("common:log:remove")
	@ResponseBody
	@PostMapping("/batchRemove")
	R batchRemove(@RequestParam("ids[]") Long[] ids) {
		int r = logService.batchRemove(ids);
		if (r > 0) {
			return R.ok();
		}
		return R.error();
	}
}
